package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;

import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Mapper
public interface OrderMapper {
    @Options(keyProperty = "id", useGeneratedKeys = true)
    @Insert("insert into orders (number, user_name, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark,  phone, address, consignee, estimated_delivery_time, delivery_status, pack_amount, " +
            "tableware_number, tableware_status)\n" +
            "        values (#{number},#{userName}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod},\n" +
            "                #{payStatus}, #{amount}, #{remark}, #{phone}, #{address}, #{consignee},\n" +
            "                #{estimatedDeliveryTime}, #{deliveryStatus}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus})")
    void insert(Orders orders);

    Page<OrderVO> historyOrders(@Param("userId") Long userId,@Param("dto") OrdersPageQueryDTO dto);

    @Select("select * from orders where id = #{id}")
    OrderVO historyOrdersByOrderId(Long id);

    @Update("update orders set status = 6 where id = #{id}")
    void cancel(Long id);


    @Select("select count(*) from orders where status= #{status}")
//    @Select("select status,count(*) from orders where status= 1 or status= 3 or status =4  group by status")
    Integer statistics(Integer toBeConfirmed);

//    @Update("update orders set status =#{confirmed} where id = #{id}")
    void updateStatus(Orders order);


//    @Select("select * from orders where status=#{pendingPayment} and order_time<#{orderTime}")
    @Select("select * from orders where status=#{pendingPayment} and order_time<#{orderTime}")
    List<Orders> queryTimeOutOrders(Integer pendingPayment, LocalDateTime orderTime);

    void updateBatch(LocalDateTime cancelTime,LocalDateTime deliveryTime, Integer status, String cancelReason, List<Long> idList);

    Double sumByMap(Map map);


    Integer countByMap(Map map);

    List<GoodsSalesDTO> top10(LocalDateTime beginTime, LocalDateTime endTime);
}
